<script setup>
import { ref, onMounted } from 'vue'
import { useRoute } from 'vue-router'
import { ElMessage } from 'element-plus'
import { findDetailAPI, findRelevantGoodsAPI } from '@/apis/detail.js'
import { useCartStore } from '@/stores/cartStore.js'
import ImageView from '@/components/ImageView/index.vue'
import GoodsSku from '@/components/GoodsSku/index.vue'
import DetailHot from './components/DetailHot.vue'

// 获取产品详情数据
const goods = ref({})
const route = useRoute() // useRoute 获取路由参数
// 定义请求函数
const getDetail = async () => {
  const res = await findDetailAPI(route.params.id)
  goods.value = res.result
  console.log('产品详情',res)
}
// 生命周期函数
onMounted(() => getDetail())

// 获取同类商品推荐数据
const relevantList = ref([])
// 定义请求函数
const getRelevant = async () => {
  const res = await findRelevantGoodsAPI(route.params.id)
  relevantList.value = res.result
  console.log('推荐商品',res)
}
// 生命周期函数
onMounted(() => getRelevant())

// 点击切换Tab
const tablist = ref([
  {id: 0, name: '商品详情', count: null},
  {id: 1, name: '商品评价', count: 0}
])
const activeIndex = ref(0)
const getTab = (index) => {
  console.log('索引',index)
  activeIndex.value = index
}

// sku规格被操作时
let skuObj = {}
const skuChange = (sku) => {
  console.log(sku)
  skuObj = sku
}

// count数量被操作时
const count = ref(1)
const countChange = (count) => {
  console.log(count)
}

// 点击添加购物车按钮
const cartStore = useCartStore()
const addCart = () => {
  if(skuObj.skuId) {
    // 规格已经选择 触发action
    cartStore.addCart({
      id: goods.value.id, // 商品id
      name: goods.value.name, // 商品名称
      picture: goods.value.mainPictures[0], // 商品图片
      price: goods.value.price, // 最新价格
      count: count.value, // 商品数量
      skuId: skuObj.skuId, // skuId
      attrsText: skuObj.specsText, // 商品规格文本
      selected: true // 商品是否选中
    })
    ElMessage.success('加入购物车成功')
  } else {
    // 规格没有选择 提示用户选择
    ElMessage.error('请选择规格')
  }
}
</script>

<template>
  <div class="app-details">
    <!--报错解决方案2 v-if="goods.details"有数据才往下渲染-->
    <div class="container" v-if="goods.details">
      <!--面包屑-->
      <div class="bread-group">
        <el-breadcrumb separator=">">
          <el-breadcrumb-item :to="{path:'/'}">首页</el-breadcrumb-item>
          <!--
            错误原因：goods一开始{} {}.categories -> undefined -> undefined[1]
            1.可选链的语法?.
            2.v-if手动控制渲染时机 保证只有数据存在才渲染
          -->
          <el-breadcrumb-item :to="{path:`/category/${goods.categories[1].id}`}">{{goods.categories[1].name}}</el-breadcrumb-item>
          <el-breadcrumb-item :to="{path:`/category/${goods.categories[0].id}`}">{{goods.categories[0].name}}</el-breadcrumb-item>
          <el-breadcrumb-item>{{goods.name}}</el-breadcrumb-item>
        </el-breadcrumb>
      </div>
      <div class="goods-detail">
        <div class="goods-media">
          <!--<div class="goods-slide"></div>-->
          <ImageView :image-list="goods.mainPictures"></ImageView>
          <div class="goods-sales">
            <ul>
              <li>
                <p>销量人气</p>
                <p>{{goods.salesCount}}+</p>
                <p><i class="iconfont icon-task-filling"></i>销量人气</p>
              </li>
              <li>
                <p>商品评价</p>
                <p>{{goods.commentCount}}+</p>
                <p><i class="iconfont icon-comment-filling"></i>查看评价</p>
              </li>
              <li>
                <p>收藏人气</p>
                <p>{{goods.collectCount}}+</p>
                <p><i class="iconfont icon-favorite-filling"></i>收藏商品</p>
              </li>
              <li>
                <p>品牌信息</p>
                <p>{{goods.brand.name}}+</p>
                <p><i class="iconfont icon-dynamic-filling"></i>品牌主页</p>
              </li>
            </ul>
          </div>
        </div>
        <div class="goods-info">
          <h2 class="g-name">{{goods.name}}</h2>
          <p class="g-desc">{{goods.desc}}</p>
          <p class="g-price">
            <span class="n">{{goods.oldPrice}}</span>
            <span class="o">{{goods.price}}</span>
          </p>
          <div class="g-service">
            <dl>
              <dt>促销</dt>
              <dd>12月好物放送，App领券购买直降120元</dd>
            </dl>
            <dl>
              <dt>配送</dt>
              <dd>
                <b style="vertical-align: middle; padding-right: 5px; font-weight: normal;">至</b>
                <div class="city-group"></div>
              </dd>
            </dl>
            <dl>
              <dt>服务</dt>
              <dd>
                <span>无忧退货</span>
                <span>快速退款</span>
                <span>免费包邮</span>
                <a href="javascript:;">了解详情</a>
              </dd>
            </dl>
          </div>
          <!--sku组件-->
          <GoodsSku :goods="goods" @change="skuChange"></GoodsSku>
          <!--数量组件-->
          <div class="g-number">
            <div class="label">数量</div>
            <el-input-number v-model="count" :min="1" @change="countChange"></el-input-number>
            <!--<div class="number-group">
              <button class="mins">-</button>
              <input class="txt" autocomplete="off" />
              <button class="plus">+</button>
            </div>-->
          </div>
          <div class="g-addcart">
            <el-button @click="addCart" type="primary">加入购物车</el-button>
          </div>
        </div>
      </div>
      <div class="goods-relevant">
        <div class="head">
          <i class="icon"></i>
          <span class="tit">同类商品推荐</span>
        </div>
        <div class="goods-carousel">
          
        </div>
      </div>
      <div class="goods-footer">
        <div class="goods-article">
          <ul class="goods-tabs">
            <li v-for="(ts,index) in tablist" :key="index" @click="getTab(index)" :class="{active:index===activeIndex}"><a href="javascript:;">{{ts.name}}<span v-if="ts.count != null">({{ts.count}})</span></a></li>
          </ul>
          <div class="tab-content">
            <div class="tab-pane goods-spec" v-show="activeIndex===0">
              <ul class="attrs">
                <li v-for="item in goods.details.properties" :key="item.value">
                  <span class="dt">{{item.name}}</span>
                  <span class="dd">{{item.value}}</span>
                </li>
              </ul>
              <div class="cont">
                <img v-for="img in goods.details.pictures" :key="img" :src="img" />
              </div>
            </div>
            <div class="tab-pane goods-comment" v-show="activeIndex===1">
              <div style="padding: 40px;">暂无内容!</div>
            </div>
          </div>
          <div class="goods-warn">
            <h3>注意事项</h3>
            <p class="tit">• 购买运费如何收取？</p>
            <p> 单笔订单金额(不含运费)满88元免邮费；不满88元，每单收取10元运费。（港澳台地区需满500元免邮费；不满500元，每单收取30元运费) </p>
            <br>
            <p class="tit">• 使用什么快递发货?</p>
            <p>默认使用顺丰快递发货(个别商品使用其他快递）</p>
            <p>配送范围覆盖全国大部分地区(港澳台地区除外）</p>
            <br>
            <p class="tit">• 如何申请退货?</p>
            <p> 1.自收到商品之日起30日内，顾客可申请无忧退货，退款将原路返还，不同的银行处理时间不同，预计1-5个工作日到账； </p>
            <p>2.内裤和食品等特殊商品无质量问题不支持退货；</p>
            <p> 3.退货流程： 确认收货-申请退货-客服审核通过-用户寄回商品-仓库签收验货-退款审核-退款完成； </p>
            <p> 4.因小兔鲜儿产生的退货，如质量问题，退货邮费由小兔鲜儿承担，退款完成后会以现金券的形式报销。因客户个人原因产生的退货，购买和寄回运费由客户个人承担。 </p>
          </div>
        </div>
        <div class="goods-aside">
          <!--24小时热销榜-->
          <DetailHot :hot-type="1"></DetailHot>
          <!--周热销榜-->
          <DetailHot :hot-type="2"></DetailHot>
        </div>
      </div>
    </div>
  </div>
</template>

<style lang="scss" scoped>
.app-details{
  padding-bottom: 40px;
  .bread-group{
    display: flex;
    align-items: center;
    padding: 25px 0;
    .iconfont{
      font-size: 12px;
      margin-left: 5px;
      margin-right: 5px;
      line-height: 22px;
    }
    a:hover{
      color: $xtxColor;
    }
    :deep() {
      .el-breadcrumb__separator{
        font-family: monospace;
      }
    }
  }
}
.goods-detail{
  background-color: #fff;
  padding: 30px 50px;
  display: flex;
  .goods-media{
    width: 480px;
    flex-shrink: 0;
    .goods-slide{
      width: 100%;
      height: 400px;
      display: flex;
      position: relative;
      z-index: 10;
    }
    .goods-sales{
      max-width: 400px;
      margin-top: 40px;
      ul{
        display: flex;
        align-items: center;
        text-align: center;
        li{
          flex: 1;
          position: relative;
          &::after{
            content: '';
            position: absolute;
            top: 10px;
            left: 0;
            height: 60px;
            border-left: 1px solid #e4e4e4;
          }
          &:first-child::after{
            display: none;
          }
          p:first-child{
            color: #999;
          }
          p:nth-child(2){
            color: $priceColor;
            margin-top: 10px;
          }
          p:last-child{
            color: #666;
            margin-top: 10px;
            i{
              color: $xtxColor;
              font-size: 14px;
              margin-right: 2px;
            }
            &:hover{
              color: $xtxColor;
              cursor: pointer;
            }
          }
        }
      }
    }
  }
  .goods-info{
    flex: 1 1 auto;
    padding-left: 70px;
    padding-right: 20px;
    .g-name{
      font-size: 22px;
      font-weight: 700;
      color: #333;
    }
    .g-desc{
      font-size: 14px;
      color: #999;
    }
    .g-price{
      margin-top: 10px;
      span{
        font-size: 22px;
        color: $priceColor;
        margin-right: 10px;
        &:last-child{
          font-size: 16px;
          color: #999;
          text-decoration: line-through;
        }
      }
      span::before{
        content: '￥';
        font-size: 14px;
      }
    }
    .g-service{
      background-color: #f5f5f5;
      padding: 20px 10px 5px 10px;
      margin-top: 10px;
      dl{
        display: flex;
        align-items: center;
        margin-bottom: 20px;
        dt{
          width: 50px;
          color: #999;
        }
        dd{
          color: #666;
          &:last-child span{
            margin-right: 10px;
          }
          &:last-child span::before{
            content: '•';
            color: $xtxColor;
            margin-right: 2px;
          }
          &:last-child a{
            color: $xtxColor;
          }
        }
      }
    }
    .g-number{
      display: flex;
      align-items: center;
      margin-top: 20px;
      .label{
        width: 60px;
        color: #999;
        padding-left: 10px;
      }
      .number-group{
        width: 120px;
        height: 30px;
        display: flex;
        border: 1px solid #e4e4e4;
        .plus,.mins{
          width: 29px;
          line-height: 28px;
          text-align: center;
          background-color: #f8f8f8;
          font-size: 16px;
          color: #666;
          cursor: pointer;
          &[disabled]{
            opacity: .5;
            cursor: default;
          }
        }
        .plus{
          border-left: 1px solid #e4e4e4;
        }
        .mins{
          border-right: 1px solid #e4e4e4;
        }
        .txt{
          width: 60px;
          padding: 0 5px;
          text-align: center;
          color: #666;
          border: none;
          outline: none;
        }
      }
    }
    .g-addcart{
      padding-top: 20px;
      padding-left: 10px;
      :deep() {
        .el-button{
          width: 180px;
          height: 50px;
          font-size: 16px;
          background-color: $xtxColor;
          border-color: $xtxColor;
        }
      }
    }
  }
}
.goods-relevant{
  background-color: #fff;
  margin-top: 20px;
  .head{
    height: 80px;
    line-height: 80px;
    padding: 0 20px;
    .icon{
      width: 16px;
      height: 16px;
      display: inline-block;
      border-top: 4px solid #27ba9b;
      border-right: 4px solid #27ba9b;
      box-sizing: border-box;
      position: relative;
      transform: rotate(45deg);
      &:before{
        content: "";
        width: 10px;
        height: 10px;
        position: absolute;
        left: 0;
        top: 2px;
        background-color: #bcf1e6;
      }
    }
    .tit{
      font-size: 20px;
      padding-left: 10px;
    }
  }
}
.goods-footer{
  display: flex;
  margin-top: 20px;
  .goods-article{
    flex: 1 1 auto;
    padding-right: 20px;
    .goods-tabs{
      height: 70px;
      line-height: 70px;
      display: flex;
      border-bottom: 1px solid #f5f5f5;
      background-color: #fff;
      li{
        font-size: 18px;
        position: relative;
        &:first-child{
          border-right: 1px solid #f5f5f5;
        }
        &.active::before{
          content: "";
          position: absolute;
          left: 40px;
          bottom: -1px;
          width: 72px;
          height: 2px;
          background-color: $xtxColor;
        }
        a{
          display: block;
          width: 100%;
          height: 100%;
          padding: 0 40px;
        }
        span{
          color: $priceColor;
          font-size: 16px;
          margin-left: 10px;
        }
      }
    }
    .tab-content{
      background-color: #fff;
      .goods-spec{
        padding: 40px;
        .attrs{
          display: flex;
          flex-wrap: wrap;
          margin-bottom: 30px;
          li{
            width: 50%;
            display: flex;
            margin-bottom: 10px;
            .dt{
              width: 100px;
              color: #999;
            }
            .dd{
              flex: 1;
              color: #666;
            }
          }
        }
        .cont{
          text-align: center;
          img{
            width: 100%;
          }
        }
      }
    }
  }
  .goods-aside{
    width: 280px;
    flex-shrink: 0;
  }
  .goods-warn{
    margin-top: 20px;
    padding-bottom: 40px;
    background-color: #fff;
    h3{
      height: 70px;
      line-height: 70px;
      border-bottom: 1px solid #f5f5f5;
      padding-left: 50px;
      font-size: 18px;
      font-weight: 400;
      margin-bottom: 10px;
    }
    p{
      line-height: 40px;
      padding: 0 25px;
      color: #666;
      &.tit{
        color: #333;
      }
    }
  }
}
</style>